package com.tiantianfresh.admin.controller;

import com.tiantianfresh.model.pojo.AJAXResult;
import com.tiantianfresh.model.pojo.Permission;
import com.tiantianfresh.model.pojo.User;
import com.tiantianfresh.admin.service.PermissionService;
import com.tiantianfresh.admin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @author FANGAO
 * @date 2021/11/8 - 下午 20:18
 */
@Controller
public class DispatcherController {
    @Autowired
    private UserService userService;
    @Autowired
    private PermissionService permissionService;
    @RequestMapping("/login")
    public String goLogin(){
        return "login";
    }
    @RequestMapping("/doAJAXLogin")
    @ResponseBody
    public Object goMain(User user, HttpSession session){
        //获取表单参数
        //1.HttpServletRequest
        //2.通过方法参数传递到方法中，
        //  条件表单中对应的input的name名称要与方法参数名相同，
        //  就是将表单封装为实体类
        User dbUser = userService.queryUserLogin(user);
        AJAXResult ar = new AJAXResult();
        if (dbUser!=null){
            session.setAttribute("loginUser",dbUser);
            List<Permission> permissionList = permissionService.queryUserPermission(dbUser.getId());
            Permission root = null;
            Map<Integer,Permission> map = new HashMap<Integer,Permission>();
            Set<String> uriSet = new HashSet<String>();
            for (Permission p : permissionList) {
                map.put(p.getId(),p);
                if (p.getUrl()!=null && !"".equals(p.getUrl())) {
                    uriSet.add(session.getServletContext().getContextPath()+p.getUrl());
                }
            }
            session.setAttribute("authUriSet",uriSet);
            for (Permission p : permissionList) {
                Permission child = p;
                if (child.getPid() == 0){
                   root = child;
                }else{
                    Permission parent = map.get(child.getPid());
                    parent.getChildren().add(child);
                }
            }
            session.setAttribute("permissionRoot",root);
            ar.setSuccess(true);
        }else{
             ar.setSuccess(false);
        }
        return ar;
    }
    @RequestMapping("/index")
    public String goIndex(){
        return "index";
    }
    @RequestMapping("/main")
    public String goMain(){
        return "main";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        //使session失效
        session.invalidate();
        return "login";
    }
    @RequestMapping("/goindex/{identity}")
    public String goindex(@PathVariable Integer identity, Model model){
        model.addAttribute("identity",identity);
        return "user/index";
    }
    @RequestMapping("/goRole")
    public String goRole(){
        return "role/index";
    }
    @RequestMapping("/goError")
    public String goError(){
        return "error";
    }

}
